package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.po.dspdbinfo.DspDbInfoPo;
import com.bwda.dsrs.filecenter.domain.condition.dspdbinfo.DspDbInfoCondition;
import com.bwda.dsrs.filecenter.domain.form.dspdbinfo.DspDbInfoQueryForm;
import com.bwda.dsrs.filecenter.domain.form.dspdbinfo.DspDbInfoUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.dspdbinfo.DspDbInfoCreateForm;
import com.bwda.dsrs.filecenter.domain.vo.dspdbinfo.DspDbInfoVo;
import com.bwda.dsrs.filecenter.service.DspDbInfoService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;
/**
 * @author Administrator
 */
@RestController
@RequestMapping("/dspdbinfo")
@Api(value = "/dspdbinfo", tags = "数据库信息表")
public class DspDbInfoController extends BaseController {

	@Autowired
	private DspDbInfoService dspDbInfoService;

	@ApiOperation(value = "查询数据库信息表",notes = "查询数据库信息表",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DspDbInfoVo> query(@ApiParam(value = "id", required = true)@RequestParam Integer dbId) throws BusinessServiceException {
		DspDbInfoPo po = dspDbInfoService.queryWithValid(dbId);
		DspDbInfoVo vo = CopyUtil.transfer(po, DspDbInfoVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询数据库信息表数量",notes = "查询数据库信息表数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid DspDbInfoQueryForm form) throws BusinessServiceException {
		DspDbInfoCondition condition = this.getConditionByQueryForm(form);
		int count = dspDbInfoService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询数据库信息表列表",notes = "查询数据库信息表列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<DspDbInfoVo>> queryList(@ModelAttribute@Valid DspDbInfoQueryForm form) throws BusinessServiceException {
		DspDbInfoCondition condition = this.getConditionByQueryForm(form);
		List<DspDbInfoPo> poList = dspDbInfoService.queryList(condition);
		List<DspDbInfoVo> voList = CopyUtil.transfer(poList, DspDbInfoVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询数据库信息表列表(带分页)",notes = "查询数据库信息表列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<DspDbInfoVo>> queryPageList(@ModelAttribute@Valid DspDbInfoQueryForm form) throws BusinessServiceException {
		DspDbInfoCondition condition = this.getConditionByQueryForm(form);
		List<DspDbInfoVo> voList = new ArrayList<>();
		int count = dspDbInfoService.queryCount(condition);
		if (count > 0) {
			List<DspDbInfoPo> poList = dspDbInfoService.queryList(condition);
			voList = CopyUtil.transfer(poList, DspDbInfoVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增数据库信息表",notes = "新增数据库信息表",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<DspDbInfoVo> add(@ModelAttribute@Valid DspDbInfoCreateForm form) throws BusinessServiceException {
		DspDbInfoPo po = CopyUtil.transfer(form, DspDbInfoPo.class);
//		po.setDbId(RandomUtils.nextInt());
		dspDbInfoService.insert(po);
		DspDbInfoVo vo = CopyUtil.transfer(po, DspDbInfoVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改数据库信息表",notes = "修改数据库信息表",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid DspDbInfoUpdateForm form) throws BusinessServiceException {
		DspDbInfoPo po = CopyUtil.transfer(form, DspDbInfoPo.class);
		dspDbInfoService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除数据库信息表",notes = "删除数据库信息表",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "id", required = true)@RequestParam Integer dbId) throws BusinessServiceException {
		dspDbInfoService.delete(dbId);
		return getSuccessResult();
	}

	/**
	 * DspDbInfoQueryForm转换为DspDbInfoCondition
	 *
	 * @param form
	 * @return
	 */
	private DspDbInfoCondition getConditionByQueryForm(DspDbInfoQueryForm form) {
		DspDbInfoCondition condition = CopyUtil.transfer(form, DspDbInfoCondition.class);
		return condition;
	}

}